Skip to content

Add label_mapping support to TokenClassificationEvaluator#772

Open
SuryaSunil1326 wants to merge 1 commit into
huggingface:mainfrom
SuryaSunil1326:fix/token-classification-label-mapping
Open

Add label_mapping support to TokenClassificationEvaluator#772
SuryaSunil1326 wants to merge 1 commit into
huggingface:mainfrom
SuryaSunil1326:fix/token-classification-label-mapping

Conversation

@SuryaSunil1326

Copy link
Copy Markdown

What this PR does

Fixes #545.

The documentation for TokenClassificationEvaluator.compute() lists label_mapping as an accepted parameter, but it was never implemented — unlike TextClassificationEvaluator and ImageClassificationEvaluator, which both support it.

This PR adds the missing label_mapping parameter to both compute() and predictions_processor(), allowing users to map pipeline output labels (e.g. "LABEL_0", "B-LOC") to the label names expected by the evaluation metric.

Changes

  • src/evaluate/evaluator/token_classification.py: added label_mapping: Optional[Dict[str, str]] = None to compute() and predictions_processor(); mapping is applied per-token using .get(entity, entity) so unmapped labels pass through unchanged
  • tests/test_evaluator.py: added test_predictions_processor_with_label_mapping (unit test for the mapping logic directly) and test_label_mapping_in_compute (end-to-end test using a dataset with alternative label names)

Testing

pytest tests/test_evaluator.py -k "token" -v

New tests added:

  • test_predictions_processor_with_label_mapping — passes locally
  • test_label_mapping_in_compute — requires TF (same environment constraint as existing test_class_init and test_overwrite_default_metric); passes in CI

Code generated with Claude's help and reviewed by me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Documentation claims that label_mapping is an accepted input to TokenClassificationEvaluator.compute(); it is not

1 participant